package com.cysion.study.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.uibox.dialog.CONFIRM
import com.cysion.study.R
import com.cysion.study.adapter.PrivateManageAdapter
import com.cysion.study.adapter.PrivateManageAdapter.Companion.del
import com.cysion.study.adapter.PrivateManageAdapter.Companion.edit
import com.cysion.study.adapter.PrivateManageAdapter.Companion.preview
import com.cysion.study.entity.ManagesEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_private_manage.*
//家教信息管理
class PrivateManageActivity : BaseActivity(), View.OnClickListener {


    override fun getLayoutId(): Int = R.layout.activity_private_manage

    private lateinit var dataAdapter: PrivateManageAdapter
    private val dataList: MutableList<ManagesEntity> = mutableListOf()
    private var curPage = 1

    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("家教信息管理")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()
        tv_info.setOnClickListener(this)
        tv_submit.setOnClickListener(this)
    }
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_info->{//自我介绍/
                var bundle = Bundle()
                bundle.putSerializable("obj", dataList.get(0))
                startActivity_ex<TeacherInfoActivity>("key", bundle)//教师资历
            }
            R.id.tv_submit->{//增加家教信息
                var bundle = Bundle()
                bundle.putString("type", "1")//type 1新增 2编辑
                startActivity_ex<PrviewEditActivity>("key", bundle)
            }
        }
    }
    //加载数据
    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList(curPage)
            smartLayout.setEnableLoadMore(true)

        }
        smartLayout.setOnLoadMoreListener {
            getDataList(curPage)
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        dataAdapter = PrivateManageAdapter(dataList, Box.context)
        rv_list.adapter = dataAdapter
        rv_list.layoutManager = LinearLayoutManager(Box.context)
        dataAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == preview) {//预览
                var bundle = Bundle()
                bundle.putSerializable("obj", obj)
                startActivity_ex<PrviewActivity>("key", bundle)
            }else if (flag == edit) {//编辑
                var bundle = Bundle()
                bundle.putSerializable("obj", obj)
                bundle.putString("type", "2")//type 1新增 2编辑
                startActivity_ex<PrviewEditActivity>("key", bundle)
            }else if (flag == del) {//删除
                Alert.normal(self, "提示", "确认删除吗？") { type, msg ->
                    if (type == CONFIRM) {
                        delCol(obj.id,position)
                    }
                }
            }
        }

    }

    private fun delCol(id: String, position: Int) {
        Alert.loading(self)
        ApiClient.api.delInfo(UserCache.token,id).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    ShowToast("删除成功")
                    dataAdapter.notifyItemRemoved(position)
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }

    }

    private fun getDataList(page: Int) {
        Alert.loading(self)
        ApiClient.api.getManageInfo(UserCache.token,curPage.toString()).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    if (curPage == 1) {
                        dataList.clear()
                        if (it.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        dataAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }

                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    stopRefresh()
                }
            }

    }
    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }


    override fun closeMvp() {
    }

}
